<template>
  <div class="search">
    <div class="search-box-wrapper">
      <search-box ref="searchBox"
                  @query="onQueryChange"></search-box>
    </div>
    <div class="shortcut-wrapper"
         v-show="!query" ref="shortcutWrapper">
      <scroll :data="shortcuts" ref="shortcuts" class="shortcut">
        <div>
          <div class="hot-key">
            <h1 class="title">热门搜索</h1>
            <ul>
              <li v-for="item in hotkeys"
                  :key="item.n"
                  @click="setQuery(item.k)">
                {{item.k}}
              </li>
            </ul>
          </div>
          <div class="search-history" v-show="searchHistory.length">
            <h1 class="title">
              <span class="text">搜索历史</span>
              <span class="clear" @click.stop="showConfirm"><i class="icon-clear"></i></span>
            </h1>
            <search-list :searches="searchHistory" @select="setSearch" @delete="deleteSearchHistory"></search-list>
          </div>
        </div>
      </scroll>
    </div>
    <div class="search-result"
         v-show="query"
         ref="searchResult">
      <suggest :query="query"
               @scrollStarted="scrollStarted"
               @select="saveSearch"
               ref="suggest"></suggest>
    </div>
    <confirm ref="confirm" text="是否清空搜索历史?" @confirm="clearSearchHistory"></confirm>
    <router-view></router-view>
  </div>
</template>

<script>
import SearchBox from 'base/search-box/search-box'
import { getHotKey } from 'api/search'
import { ERR_OK } from 'api/config'
import Suggest from 'components/suggest/suggest'
import { playlistMixin, searchMixin } from 'common/js/mixin'
import {mapActions, mapGetters} from 'vuex'
import SearchList from 'base/search-list/search-list'
import Confirm from 'base/confirm/confirm'
import Scroll from '../../base/scroll/scroll'

export default {
  name: 'search',
  components: { Scroll, Suggest, SearchBox, SearchList, Confirm },
  data () {
    return {
      hotkeys: []
    }
  },
  computed: {
    ...mapGetters(['searchHistory']),
    shortcuts () {
      return this.hotkeys.concat(this.searchHistory)
    }
  },
  mixins: [playlistMixin, searchMixin],
  methods: {
    handlePlaylist (playlist) {
      const bottom = playlist.length > 0 ? '60px' : ''
      this.$refs.searchResult.style.bottom = bottom
      this.$refs.shortcutWrapper.style.bottom = bottom
      setTimeout(() => {
        this.$refs.suggest.refresh()
        this.$refs.shortcuts.refresh()
      }, 50)
    },
    setSearch (item) {
      this.setQuery(item)
    },
    showConfirm () {
      this.$refs.confirm.show()
    },
    _getHotKey () {
      getHotKey().then(resp => {
        if (resp.code === ERR_OK) {
          this.hotkeys = resp.data.hotkey.slice(0, 10)
        }
      })
    },
    ...mapActions([
      'saveSearchHistory',
      'deleteSearchHistory',
      'clearSearchHistory'
    ])
  },
  watch: {
    shortcuts () {
      setTimeout(() => {
        this.$refs.shortcuts.refresh()
      }, 20)
    },
    query (newVal) {
      if (!newVal) {
        setTimeout(() => {
          this.$refs.shortcuts.refresh()
        }, 20)
      }
    }
  },
  created () {
    this._getHotKey()
  }
}
</script>

<style lang="less" scoped>
@import "~common/css/variable";
@import "~common/css/mixin";
.search {
  margin: 0 15px;
  .shortcut-wrapper {
    position: fixed;
    top: 150px;
    bottom: 0;
    width: 100%;
    .shortcut {
      height: 100%;
      overflow: hidden;
    }
    .hot-key{
      .title {
        color: @color-text-d;
        font-size: @font-size-medium-x;
        font-weight: bold;
      }
      ul {
        margin-top:20px;
        li {
          display: inline-block;
          padding: 5px 8px;
          margin-bottom: 15px;
          margin-right: 15px;
          color: @color-text-d;
          background-color: @color-highlight-background;
          border-radius: 4px;
          font-size: @font-size-medium;
        }
      }
    }
    .search-history{
      margin-right: 20px;
      position: relative;
      .title {
        display: flex;
        align-items: center;
        justify-content: space-between;
        height: 40px;
        font-size: @font-size-medium;
        color: @color-text-l;
        padding-right: 10px;
        .clear {
          .extend-click;
          .icon-clear {
            font-size: @font-size-medium;
            color: @color-text-d;
          }
        }
      }
    }
  }
  .search-result {
    position: fixed;
    width: 100%;
    top: 178px;
    bottom: 0;
  }
}
</style>
